package com.yaolong.api.modules.opms.service.impl;

import com.yaolong.api.modules.opms.entity.OpmsNotice;
import com.yaolong.api.modules.opms.dao.OpmsNoticeDao;
import com.yaolong.api.modules.opms.service.OpmsNoticeService;
import com.yaolong.api.modules.sys.dto.UserInfoDto;
import com.yaolong.api.modules.sys.po.RoleInfo;
import com.yaolong.api.modules.sys.service.ISysUserInfoService;
import com.yaolong.api.modules.utils.CheckRoleUtils;
import com.yaolong.commons.base2.BaseServiceImpl;
import com.yaolong.commons.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 系统公告表 服务实现类
 * </p>
 *
 * @author yaolonga
 * @version v1.0.0
 * @since 2023-02-15
 */
@Service
public class OpmsNoticeServiceImpl extends BaseServiceImpl<OpmsNoticeDao, OpmsNotice> implements OpmsNoticeService {

    @Autowired
    private ISysUserInfoService iSysUserInfoService;

    @Override
    public IPage<OpmsNotice> page(int current, int size, OpmsNotice domain) {
        IPage<OpmsNotice> page = new Page<>(current, size);
        UserInfoDto userInfoDto = iSysUserInfoService.userInfoByContext();
        List<RoleInfo> roleInfos = MapperUtils.copyList(userInfoDto.getRoles(), RoleInfo.class);
        Long userId = userInfoDto.getUserId();
        boolean flag = CheckRoleUtils.checkRole(roleInfos);
        if (!CheckRoleUtils.checkAdminRole(roleInfos)) {
            //无管理员权限
            if (!flag) {
                //居民用户
                domain.setCreatedBy(userId);
            }
        }

        return lambdaQuery()
                .like(!ObjectUtils.isEmpty(domain.getTitile()), OpmsNotice::getTitile, domain.getTitile())
                .eq(!ObjectUtils.isEmpty(domain.getType()), OpmsNotice::getType, domain.getType())
                .eq(!ObjectUtils.isEmpty(domain.getCreatedBy()), OpmsNotice::getSendId, domain.getCreatedBy())
                .page(page);
    }

    @Override
    public List<OpmsNotice> list(OpmsNotice domain) {
        return lambdaQuery()
                .like(!ObjectUtils.isEmpty(domain.getTitile()), OpmsNotice::getTitile, domain.getTitile())
                .eq(!ObjectUtils.isEmpty(domain.getType()), OpmsNotice::getType, domain.getType())
                .list();
    }


}
